//
//  SDHSportsTabBarVC.m
//  SDHSportsNews
//
//  Created by MichaelShan on 2021/3/4.
//

#import "SDHSportsTabBarVC.h"
#import "SDHSportsHomeVC.h"
#import "SDHSportsMatchVC.h"
#import "SDHSportsInteVC.h"
#import "SDHSportsMineVC.h"
#import "SDHSportsTabbar.h"

@interface SDHSportsTabBarVC () <UITabBarDelegate>

@property (nonatomic, strong) UIColor *selectedTextColor;
@property (nonatomic, strong) SDHSportsTabbar *customTabbar;
@property (nonatomic, assign) NSInteger lastSelectedIndex;

@end

@implementation SDHSportsTabBarVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.selectedTextColor = [UIColor redColor];
    
    SDHSportsHomeVC *homeVC = [[SDHSportsHomeVC alloc] init];
    [self setViewController:homeVC title:@"Home" image:@"home_n" selectImage:@"home_s"];

    SDHSportsMatchVC *sportsVC = [[SDHSportsMatchVC alloc] init];
    [self setViewController:sportsVC title:@"Match" image:@"bisai_n" selectImage:@"bisai_s"];
    
    SDHSportsInteVC *inteVC = [[SDHSportsInteVC alloc] init];
    [self setViewController:inteVC title:@"Intelligence" image:@"qingbao_n" selectImage:@"qingbao_s"];
    
    SDHSportsMineVC *mineVC = [[SDHSportsMineVC alloc] init];
    [self setViewController:mineVC title:@"Mine" image:@"wode_n" selectImage:@"wode_s"];
    
    __weak typeof(self) weakSelf = self;
    SDHSportsTabbar *tabbar = [[SDHSportsTabbar alloc] initWithBumpIndex:_lastSelectedIndex bumpHeight:10 didTapBumpBarBlock:^(NSInteger index) {
        __strong typeof(self) strongSelf = weakSelf;
        [strongSelf setSelectedIndex:index];
    }];
    self.customTabbar = tabbar;
    
    [self setValue:self.customTabbar forKey:@"tabBar"];
}

#pragma mark - 添加子控制器
-(void)setViewController:(UIViewController *)viewController title:(NSString *)title image:(NSString *)image selectImage:(NSString *)selectImage {
    static NSInteger index = 0;
    viewController.tabBarItem.title = title;
    viewController.title = title;
    viewController.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    viewController.tabBarItem.selectedImage = [[UIImage imageNamed:selectImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    viewController.tabBarItem.tag = index;
    index++;
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:viewController];
    [self addChildViewController:nav];
    viewController.navigationController.navigationBar.hidden = YES;
    
    [viewController.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: self.selectedTextColor} forState:UIControlStateSelected];
}

#pragma - UITabbar delegate
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    SDHSportsTabbar *tab = (SDHSportsTabbar *)tabBar;
    NSInteger index = [tabBar.items indexOfObject:item];
    tab.index = index;
    [tab setNeedsLayout];
    
    // 按钮点击后，调整点击效果
    [self adjustItem:index];
}

- (void)setSelectedIndex:(NSUInteger)selectedIndex {
    
    if (self.selectedIndex != selectedIndex) {
        //设置最近一次
        _lastSelectedIndex = self.selectedIndex;
        NSLog(@"1 OLD:%ld, NEW:%ld",self.lastSelectedIndex, selectedIndex);
    }
    
    //调用父类的setSelectedIndex
    [super setSelectedIndex:selectedIndex];
}

- (void)adjustItem:(NSInteger)index {
    // 调整item点击效果
    UIBarButtonItem *tmpItem = nil;
    for (tmpItem in self.tabBar.items) {
        tmpItem.imageInsets = UIEdgeInsetsZero;
    }
    tmpItem = (UIBarButtonItem *)[self.tabBar.items objectAtIndex:index];
    tmpItem.imageInsets = UIEdgeInsetsMake(-6, 0, 0, 0);
}

@end
